package window;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;

import main.DataConnection;
import main.Its;
import main.OutcomeProblem;

import org.jdesktop.layout.GroupLayout;
import org.jdesktop.layout.LayoutStyle;

/**
 * JFrame for adding ITS to Database.
 * 
 * @author Dawid Nejman
 * @version 1.2 18-12-2011
 */
public class AddItsWindow extends JFrame {

	private static final long serialVersionUID = 6157006938134238871L;
	private JButton jButton1;
	private JButton jButton2;
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JScrollPane jScrollPane1;
	private JTable jTable1;
	private JTextField jTextField1;
	private OutcomeProblem op;

	/**
	 * Creates new form AddItsWindow
	 */
	public AddItsWindow() {
		this.op = new OutcomeProblem();
		initComponents();
	}

	/**
	 * Getter for {@code OutcomeProblem} instance variable. Used by {@code ItsCharacteristicsWindow} as a return value.
	 * 
	 * @return {@code OutcomeProblem} instance.
	 */
	public OutcomeProblem getOp() {
		return op;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 */
	private void initComponents() {

		jScrollPane1 = new JScrollPane();
		jTable1 = new JTable();
		jLabel1 = new JLabel();
		jTextField1 = new JTextField();
		jLabel2 = new JLabel();
		jButton1 = new JButton();
		jButton2 = new JButton();

		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("Add ITS");

		jTable1.setModel(new DefaultTableModel(new Object[][] {

		}, new String[] { "Problem", "Subproblem", "Size" }));
		jScrollPane1.setViewportView(jTable1);
		jLabel1.setText("ITS Name");
		jLabel2.setText("ITS Solutions");
		jButton1.setText("Add");
		jButton1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		jButton2.setText("Create ITS");
		jButton2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent evt) {
				jButton2ActionPerformed(evt);
			}
		});

		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(GroupLayout.LEADING)
				.add(layout
						.createSequentialGroup()
						.addContainerGap()
						.add(layout
								.createParallelGroup(GroupLayout.LEADING)
								.add(layout.createSequentialGroup().add(jButton1).addPreferredGap(LayoutStyle.RELATED, 195, Short.MAX_VALUE).add(jButton2))
								.add(layout
										.createSequentialGroup()
										.add(layout.createParallelGroup(GroupLayout.LEADING, false).add(jLabel1).add(jLabel2)
												.add(jScrollPane1, GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE).add(jTextField1)).add(0, 0, Short.MAX_VALUE)))
						.addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.LEADING).add(
				GroupLayout.TRAILING,
				layout.createSequentialGroup().addContainerGap().add(jLabel1).addPreferredGap(LayoutStyle.RELATED)
						.add(jTextField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).add(7, 7, 7).add(jLabel2)
						.addPreferredGap(LayoutStyle.RELATED).add(jScrollPane1, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.RELATED).add(layout.createParallelGroup(GroupLayout.BASELINE).add(jButton1).add(jButton2))
						.addContainerGap(7, Short.MAX_VALUE)));

		pack();
	}

	private void jButton1ActionPerformed(ActionEvent evt) {
		java.awt.EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {
				ItsCharacteristicsWindow icw = new ItsCharacteristicsWindow(AddItsWindow.this);
				icw.setVisible(true);
			}
		});
	}

	private void jButton2ActionPerformed(ActionEvent evt) {
		String itsName = this.jTextField1.getText();
		DefaultTableModel dtm = (DefaultTableModel) this.jTable1.getModel();
		int size = dtm.getRowCount();

		if (itsName.isEmpty()) {
			JOptionPane.showMessageDialog(this, "ITS name cannot be empty!", "Error", JOptionPane.ERROR_MESSAGE);
			return;
		} else if (DataConnection.itsExists(itsName)) {
			JOptionPane.showMessageDialog(this, "ITS with given name already exists!", "Error", JOptionPane.ERROR_MESSAGE);
			return;
		}

		Its its = new Its(itsName);

		for (int i = 0; i < size; i++) {
			String problem = (String) dtm.getValueAt(i, 0);
			String subproblem = (String) dtm.getValueAt(i, 1);
			String value = (String) dtm.getValueAt(i, 2);

			if (value.endsWith("%")) {
				int place = value.indexOf("%");
				value = value.substring(0, place);
			}
			double percent = Double.valueOf(value) / 100.0;

			its.addProblem(problem);
			its.addSubProblem(problem, subproblem, percent);
		}

		DataConnection.addIts(its);
		dispose();
	}

	/**
	 * Refreshes window table.
	 */
	public void refreshTable() {
		DefaultTableModel dtm = (DefaultTableModel) this.jTable1.getModel();
		dtm.addRow(new Object[] { this.op.getProblem(), this.op.getSubproblem(), this.op.getSize() });
	}
}